package com.sixuan.rabbitmqprovider.workmode.routing;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * RabbitMq工作模式：Routing（路由模式）
 * 1.生命一个交换机，并指定该交换机是DIRECT模式（即routing模式）
 * 2.一个交换机绑定多个队列，每个队列设置自己的routingKey，且一个队列可以设置多个routingKey。
 * 3.每个消费则监听自己的队列
 * 4.生产将消息发给指定的交换机，发送的时候需要指定routingKey，交换机来发送给指定了该routingKey的所有队列
 * Routing模式和Publish/Subscribe模式的区别：
 * 1.Publish/Subscribe模式不需要指定RoutingKey，消息会发送给与该交换机绑定的所有队列。
 * 2.当两个队列RoutingKey相同时，此时Routing模式的效果与Publish/Subscribe模式一致
 * 3.即Routing模式比Publish/Subscribe模式更强大。
 * Created by LENOVO on 2020-7-8 17:18
 *
 * @author LENOVO
 */
public class Provider {

    private static final String rabbitMqHost = "47.97.195.191";
    private static final int rabbitMqPort = 5672;
    private static final String username = "guest";
    private static final String password = "guest";

    private static final String EMAIL_QUEUE_INFORMATION = "email_queue_information";
    private static final String SMS_QUEUE_INFORMATION = "sms_queue_information";
    private static final String EXCHANGE_ROUTING = "exchange_routing";
    private static final String EMAIL_ROUTING_INFORMATION = "email_routing_information";
    private static final String SMS_ROUTING_INFORMATION = "sms_routing_information";


    public static void main(String[] args) {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost(rabbitMqHost);
        connectionFactory.setPort(rabbitMqPort);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        connectionFactory.setVirtualHost("/");

        Connection connection = null;
        Channel channel = null;
        try {
            connection = connectionFactory.newConnection();
            channel = connection.createChannel();
            //声明交换机,参数：String exchange, String type
            /**
             * 1.String exchange：交换机名称
             * 2.String type 交换机类型
             * BuiltinExchangeType:四种类型
             * 1.FANOUT:发布订阅类型（publish/subscribe）
             * 2.DIRECT:对应Routing模式
             * 3.HEADERS:对应Headers模式
             * 4.TOPIC:对应Topic模式
             */
            channel.exchangeDeclare(EXCHANGE_ROUTING, BuiltinExchangeType.DIRECT);
            //声明队列
            //email队列
            channel.queueDeclare(EMAIL_QUEUE_INFORMATION, true, false, false, null);
            //sms队列
            channel.queueDeclare(SMS_QUEUE_INFORMATION, true, false, false, null);

            //将队列绑定到交换机上,参数：String queue, String exchange, String routingKey
            /**
             * 1.String queue:队列名称
             * 2.String exchange:交换机名称
             * 3.String routingKey:路由key，交换机根据路由key将消息转发到指定队列，发布订阅模式为空串
             */
            //绑定email
            channel.queueBind(EMAIL_QUEUE_INFORMATION, EXCHANGE_ROUTING, EMAIL_ROUTING_INFORMATION);
            channel.queueBind(EMAIL_QUEUE_INFORMATION, EXCHANGE_ROUTING, "RoutingMode");
            //绑定sms
            channel.queueBind(SMS_QUEUE_INFORMATION, EXCHANGE_ROUTING, SMS_ROUTING_INFORMATION);
            channel.queueBind(SMS_QUEUE_INFORMATION, EXCHANGE_ROUTING, "RoutingMode");

            //发送消息
//            for (int i = 0; i < 9; i++) {
//                String msg = "Hello World!" + i;
//                //指定交换机就行了，并且指定routingKey
//                //指定email
//                channel.basicPublish(EXCHANGE_ROUTING, EMAIL_ROUTING_INFORMATION, null, msg.getBytes());
//                //指定sms
//                channel.basicPublish(EXCHANGE_ROUTING, SMS_ROUTING_INFORMATION, null, msg.getBytes());
//                System.out.println("Send to consumer finished ! message：" + msg);
//            }
            //为了测试Routing模式比Publish/Subscribe模式更强大
            for (int i = 0; i < 9; i++) {
                String msg = "Hello World!" + i;
                //指定交换机就行了，并且指定routingKey
                channel.basicPublish(EXCHANGE_ROUTING, "RoutingMode", null, msg.getBytes());
                System.out.println("Send to consumer finished ! message：" + msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                channel.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                connection.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
